package com.lfy.modules.common.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lfy.modules.common.domain.PageResultVO;
import org.springframework.beans.BeanUtils;

import java.util.Collections;
import java.util.List;

/**
 * 分页工具类
 *
 * @author GHQ
 * @date 2017-12-23 16:40
 */
@SuppressWarnings("unchecked")
public class PageUtil {

    public static <T> PageResultVO<T> convert2PageResult(IPage<T> page) {
        PageResultVO<T> result = new PageResultVO<>();
        result.setPageNum(page.getCurrent());
        result.setPageSize(page.getSize());
        result.setTotal(page.getTotal());
        result.setPages(page.getPages());
        result.setList(page.getRecords());
        return result;
    }

    public static <T> PageResultVO<T> convert2PageEmptyResult(IPage<T> page) {
        PageResultVO<T> result = new PageResultVO<>();
        result.setPageNum(page.getCurrent());
        result.setPageSize(page.getSize());
        result.setTotal(0L);
        result.setPages(page.getPages());
        result.setList(Collections.EMPTY_LIST);
        return result;
    }

    public static <T> PageResultVO<T> convert2PageEmptyResult(IPage<?> page, Class<T> tClass) {
        PageResultVO<T> result = new PageResultVO<>();
        result.setPageNum(page.getCurrent());
        result.setPageSize(page.getSize());
        result.setTotal(0L);
        result.setPages(page.getPages());
        result.setList(Collections.EMPTY_LIST);
        return result;
    }




    /**
     * 转换为 pageResultDTO 对象
     *
     * @param page
     * @param sourceList list
     * @return
     * @author zcq
     * @date 2018年5月16日 下午6:05:28
     */
    public static <T, E> PageResultVO<T> convert2PageResult(IPage page, List<E> sourceList) {
        PageResultVO pageResultDTO = setPage(page);
        page.setRecords(sourceList);
        pageResultDTO.setList(sourceList);
        return pageResultDTO;
    }
    /**
     * 非原数据转换
     *
     * @author <a href="https://www.lvfeiy.top">lvfeiyang</a>
     * @date 2024/4/4 20:04
     */
    public static <T>PageResultVO<T> buildPageResultVO(IPage<?> page, List<T> sourceList){
        PageResultVO pageResultVO = new PageResultVO();
        pageResultVO.setPageNum(page.getCurrent());
        pageResultVO.setPageSize(page.getSize());
        pageResultVO.setTotal(page.getTotal());
        pageResultVO.setPages(page.getPages());
        pageResultVO.setList(sourceList);
        return pageResultVO;
    }

    private static PageResultVO setPage(IPage page) {
        PageResultVO pageResultVO = new PageResultVO();
        pageResultVO.setPageNum(page.getCurrent());
        pageResultVO.setPageSize(page.getSize());
        pageResultVO.setTotal(page.getTotal());
        pageResultVO.setPages(page.getPages());
        return pageResultVO;
    }
    public static <T> PageResultVO<T> convertPageVO(PageResultVO<?> pageResultVO, List<T> sourceList) {
        PageResultVO<T> objectPageResultVO = new PageResultVO<>();
        BeanUtils.copyProperties(pageResultVO, objectPageResultVO);
        objectPageResultVO.setList(sourceList);
        return objectPageResultVO;
    }
    public static <T> PageResultVO<T> convertEmptyPageVO(PageResultVO<?> page, Class<T> tClass) {
        PageResultVO<T> objectPageResultVO = new PageResultVO<>();
        BeanUtils.copyProperties(page, objectPageResultVO);
        objectPageResultVO.setList(Collections.EMPTY_LIST);
        return objectPageResultVO;
    }
}
